package com.xbts.screen.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.core.domain.WeProduceInformationStatistics;
import com.xbts.common.core.enums.DateTypeEnum;
import com.xbts.common.core.utils.DateUtils;
import com.xbts.common.core.utils.StringUtils;
import com.xbts.screen.domain.vo.WeProduceInformationStatisticsVO;
import com.xbts.screen.mapper.WeProduceInformationStatisticsMapper;
import com.xbts.screen.service.IWeProduceInformationStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 产能信息统计Service业务层处理
 * 
 * @author xbts
 * @date 2024-01-02
 */
@Service
public class WeProduceInformationStatisticsServiceImpl extends ServiceImpl<WeProduceInformationStatisticsMapper, WeProduceInformationStatistics> implements IWeProduceInformationStatisticsService {

    @Autowired
    private WeProduceInformationStatisticsMapper weProduceInformationStatisticsMapper;

    @Override
    public boolean task() {
        List<WeProduceInformationStatistics> task = weProduceInformationStatisticsMapper.task();
        return this.saveBatch(task);
    }

    @Override
    public Object byProductionProcesses(String productionProcesses, DateTypeEnum dateType) {
        // 获取开始时间和结束时间集合
        List<List<String>> dateList = DateUtils.getDateListByDateTypeEnum(dateType);
        Map<String, List<Object>> map = new HashMap<>();
        // X轴坐标
        List<Object> keyList = new ArrayList<>(dateList.size());
        // 产出目标
        List<Object> outputTargetList = new ArrayList<>(dateList.size());
        // 实际产出
        List<Object> produceAreaList = new ArrayList<>(dateList.size());
        // 达成率
        List<Object> completionRateList = new ArrayList<>(dateList.size());

        // 获取当前日期
        LocalDate today = LocalDate.now();
        for (List<String> date : dateList) {
            String startDate = date.get(0);
            String endDate = date.get(1);

            try {
                Date startDateKey = DateUtils.parseDate(startDate, DateUtils.YYYY_MM_DD_HH_MM_SS);
                Date endDateKey = DateUtils.parseDate(endDate, DateUtils.YYYY_MM_DD_HH_MM_SS);
                // 封装X轴坐标
                switch (dateType){
                    case DAY:
                        keyList.add(DateUtils.parseDateToStr(DateUtils.DD, startDateKey));
                        break;
                    case WEEK:
                        keyList.add(DateUtils.parseDateToStr(DateUtils.MM_DD, startDateKey) + "-" + DateUtils.parseDateToStr(DateUtils.MM_DD, endDateKey));
                        break;
                    case MONTH:
                        keyList.add(DateUtils.parseDateToStr(DateUtils.MM, startDateKey));
                        break;
                    default:
                        break;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

//            // 给定的时间
//            LocalDateTime givenDateTime = LocalDateTime.parse(startDate, DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD_HH_MM_SS));
//            // 将给定的时间转换为与当前日期相比较的日期
//            LocalDate givenDate = givenDateTime.toLocalDate();
//            // 判断给定的日期是否在今天之后
//            if (givenDate.isAfter(today)) {
//                outputTargetList.add("0");
//                produceAreaList.add("0");
//            } else {
//
//            }
            // 获取指定时间段的目标和时间产出
            List<WeProduceInformationStatisticsVO> vo = weProduceInformationStatisticsMapper.byProductionProcesses(productionProcesses, startDate, endDate);

            if(vo.size() <= 0) {
                outputTargetList.add(0);
                produceAreaList.add(0);
                completionRateList.add("0%");
            } else {
                WeProduceInformationStatisticsVO weProduceInformationStatisticsVO = vo.get(0);
                Double outputTarget = weProduceInformationStatisticsVO.getOutputTarget();
                outputTarget = outputTarget == null? 0 : outputTarget;
                Double produceArea = weProduceInformationStatisticsVO.getProduceArea();
                produceArea = produceArea == null? 0 : produceArea;
                outputTargetList.add(outputTarget);
                produceAreaList.add(produceArea);
                // 计算完成率
                if(produceArea != 0 && outputTarget != 0) {
                    Double completionRate = produceArea * 100 / outputTarget;
                    String completionRateResult = String.format("%.2f", completionRate);
                    completionRateList.add(completionRateResult + "%");
                } else if (outputTarget == 0) {
                    completionRateList.add("100%");
                } else {
                    completionRateList.add("0%");
                }
            }
        }
        map.put("dateList", keyList);
        map.put("outputTargetList", outputTargetList);
        map.put("produceAreaList", produceAreaList);
        map.put("completionRateList", completionRateList);
        return map;
    }

    @Override
    public Object getDetails(DateTypeEnum dateType) {
        List<String> tiemList = DateUtils.getDateByDateTypeEnum(dateType);

        List<WeProduceInformationStatisticsVO> voList = weProduceInformationStatisticsMapper.getDetails(tiemList.get(0), tiemList.get(1));
        int size = voList.size() + 1;
        Map<String, List<Object>> map = new HashMap<>();
        // X轴坐标 工序
        List<Object> productionProcessesList = new ArrayList<>(size);
        // 产出目标
        List<Object> outputTargetList = new ArrayList<>(size);
        // 实际产出
        List<Object> produceAreaList = new ArrayList<>(size);
        // 达成率
        List<Object> completionRateList = new ArrayList<>(size);
        // 从最后一条数据开始录入
        for (int i = voList.size() - 1; i >= 0; i--) {
            WeProduceInformationStatisticsVO vo = voList.get(i);
            String productionProcesses = vo.getProductionProcesses();
            Double outputTarget = vo.getOutputTarget();
            Double produceArea = vo.getProduceArea();

            // 工序
            productionProcessesList.add(productionProcesses);
            // 实际产出
            produceAreaList.add(produceArea);
            // 产出目标
            outputTargetList.add(outputTarget);

            // 计算完成率
            if(produceArea != 0 && outputTarget != 0) {
                Double completionRate = produceArea * 100 / outputTarget ;
                String completionRateResult = String.format("%.2f", completionRate);
                completionRateList.add(completionRateResult + "%");
            } else if(outputTarget == 0) {
                completionRateList.add("100%");
            } else {
                completionRateList.add("0%");
            }
        }
        // 计算总体
        productionProcessesList.add("总体");
        double produceAreaSum = produceAreaList.stream().map(v -> (Double)v).mapToDouble(Double::doubleValue).sum();
        double outputTargetSum = outputTargetList.stream().map(v -> (Double)v).mapToDouble(Double::doubleValue).sum();
        produceAreaList.add(produceAreaSum);
        outputTargetList.add(outputTargetSum);
        // 总体达成率
        if(produceAreaSum != 0 && outputTargetSum != 0) {
            Double completionRateSum = produceAreaSum * 100 / outputTargetSum;
            String completionRateResult = String.format("%.2f", completionRateSum);
            completionRateList.add(completionRateResult + "%");
        } else if(outputTargetSum == 0) {
            completionRateList.add("100%");
        } else {
            completionRateList.add("0%");
        }


        // 倒转集合排序
        Collections.reverse(productionProcessesList);
        Collections.reverse(outputTargetList);
        Collections.reverse(produceAreaList);
        Collections.reverse(completionRateList);
        // 封装并返回
        map.put("productionProcessesList", productionProcessesList);
        map.put("outputTargetList", outputTargetList);
        map.put("produceAreaList", produceAreaList);
        map.put("completionRateList", completionRateList);
        return map;
    }
}
